//
//  Group.m
//  Untitled
//
//  Created by  user on 11-2-27.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "StaticGroup.h"

#import <QuartzCore/QuartzCore.h>

@implementation CStaticGroup
@synthesize m_titleactualsize;
@synthesize m_title;

-(id)initWithFrame:(CGRect)frame title:(NSString*)szTitle
{
    return [self initWithFrame:frame title:szTitle maxtitlewidth:0];
}

-(id)initWithFrame:(CGRect)frame title:(NSString*)szTitle maxtitlewidth:(float)nMaxtitlewidth
{
    [super initWithFrame:frame];
	self.backgroundColor=[UIColor clearColor];
	
	int nWidth=frame.size.width-GROUPRADIUS*3; 
	CGSize size=[szTitle stringSizeWithFont:[UIFont systemFontOfSize:16]forConstrainSize:CGSizeMake(nWidth, INT_MAX)];
    m_titleactualsize=CGSizeMake(MIN(size.width,nMaxtitlewidth<=0?size.width:nMaxtitlewidth),size.height);
	m_title=[[UILabel alloc]initWithFrame:CGRectMake(GROUPRADIUS+GROUPRADIUS/2,0,m_titleactualsize.width,m_titleactualsize.height)];
	m_title.font=[UIFont systemFontOfSize:16];
    m_title.backgroundColor=[UIColor clearColor];
	m_title.lineBreakMode=NSLineBreakByTruncatingTail;
	m_title.text=szTitle;
	
	[self addSubview:m_title];
	
	CGRect f=self.frame;
	m_nY=f.origin.y+size.height/2;
    
    return self;
}


- (void)drawRect:(CGRect)rect
{
    rect.origin.y+=m_nY;
	rect.size.height-=m_nY;
	CGFloat radius=GROUPRADIUS;
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,0.5,0.5,0.5,1);
    CGContextSetLineWidth(context,1);
    CGFloat minx=CGRectGetMinX(rect)+1,midx=CGRectGetMidX(rect),maxx=CGRectGetMaxX(rect)-1;
    CGFloat miny=CGRectGetMinY(rect)+1,midy=CGRectGetMidY(rect),maxy=CGRectGetMaxY(rect)-1;
//    CGContextMoveToPoint(context, minx, midy);
//    CGContextAddArcToPoint(context,minx,miny,midx,miny,radius);
//    CGContextAddArcToPoint(context,maxx,miny,maxx,midy,radius);
//    CGContextAddArcToPoint(context,maxx,maxy,midx,maxy,radius);
//    CGContextAddArcToPoint(context,minx,maxy,minx,midy,radius);

    CGContextBeginPath(context);
    CGContextMoveToPoint(context,CGRectGetMaxX(m_title.frame),miny);
    CGContextAddArcToPoint(context,maxx,miny,maxx,midy,radius);
    CGContextAddArcToPoint(context,maxx,maxy,midx,maxy,radius);
    CGContextAddArcToPoint(context,minx,maxy,minx,midy,radius);
    CGContextAddArcToPoint(context,minx,miny,CGRectGetMinX(m_title.frame),miny,radius);
    CGContextAddLineToPoint(context,CGRectGetMinX(m_title.frame),miny);
//    CGContextClosePath(context);
    CGContextStrokePath(context);
}


- (void)dealloc 
{
	[m_title release];
    [super dealloc];
}


@end
